package com.shiguo365.market.presenter.implement;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.view.IAddressAddView;
import com.shiguo365.market.display.view.IAddressListView;
import com.shiguo365.market.presenter.contract.IAddressListPresenter;

import java.util.List;

import biz.yfsoft.api.ae.api.AEClient;
import biz.yfsoft.api.ae.api.AEParam;
import biz.yfsoft.api.ae.api.BaseAECallback;

/**
 * Created by caomenglong on 16/8/12.
 */
public class AddressListPresenter implements IAddressListPresenter {
    private static final String TAG = "AddressListPresenter";
    private final Activity mActivity;
    private final IAddressListView mAddressListView;
    private String mold=null;

    public AddressListPresenter(@NonNull Activity activity, @NonNull IAddressListView addressListViewView) {
        this.mActivity = activity;
        this.mAddressListView = addressListViewView;
    }

    public void setMold(String s){
        this.mold=s;
    }

    @Override
    public void onOrderConfrimSelectedOK() {

        mAddressListView.OnOrderConfirmSelectedOK();
    }

    public String getMold(){
        return mold;
    }

    @Override
    public void getAddressListByUI(Integer uid) {
        AEClient _client = new AEClient("shiguo.user.getAddress");
        AEParam aeParam=new AEParam();
        aeParam.set("uid",uid);
        _client.call(aeParam,new BaseAECallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(List<JSONObject> jsonObjectList) {
                //Log.d(TAG,jsonObjectList.toString());
                mAddressListView.OnGetAddressListOK(jsonObjectList);
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno);
            }

            @Override
            public void onFinally(String s) {
                mAddressListView.OnGetAddressListFinsh();
            }
        });

    }

    @Override
    public void delAddressById(Integer id) {
        AEClient _client = new AEClient("shiguo.user.updateAddress");
        AEParam aeParam=new AEParam();
        aeParam.set("uid", MyApplication.getUID());
        aeParam.set("aid",id);
        aeParam.set("clear",1);
        _client.call(aeParam,new BaseAECallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(JSONObject jsonObjectList) {
                Log.d(TAG,jsonObjectList.toString());
                mAddressListView.delAddressByIdOK();
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno);
            }

            @Override
            public void onFinally(String s) {


            }
        });
    }

    @Override
    public void setAddressDefaultById(Integer id) {
        AEClient _client = new AEClient("shiguo.user.updateAddress");
        AEParam aeParam=new AEParam();
        aeParam.set("uid", MyApplication.getUID());
        aeParam.set("aid",id);
        aeParam.set("is_default",1);
        _client.call(aeParam,new BaseAECallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(JSONObject jsonObjectList) {
                Log.d(TAG,jsonObjectList.toString());
                mAddressListView.setAddressDefaultByIdOK();
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno);
            }

            @Override
            public void onFinally(String s) {

            }
        });
    }
}
